import { IFeidaoAiNodejsComponent } from '@feidao/web/interfaces';

import render from '@feidao/web/atom/nodejs/render/render';
import nodejs from '@feidao/web/atom/nodejs/msg-b/nodejs';

import server_render from '../atom-n/server-render';
import { Params } from '../atom/params2query';
import p2q from '../atom/params2query';
import generate_query4render from '../atom/generate-query4render';
import generate_sort_fields from '../atom/generate-sort-fields';

import P02 from './P02';
import P04 from './P04';
import P03 from './P03';
import P05 from './P05';
import P01 from './P01';

const name2title = {
	'staff_name': '员工名称',
	'deptname': '部门',
	'staff_type': '员工类型',
	'personal_skill': '技能名称',
	'is_special_job': '是否特种专业',
	'resume_grade': '级别',
	'is_skill_certificate': '是否技能证',
	'date_of_issue': '发证日期',
	'valitydate': '有效期',
	'enddate': '到期日期',
	'org_name': '发证机构',
	'post_name': '适用职务',
	'memo': '备注'
};

function create_data_sort(fd: IFeidaoAiNodejsComponent) {
	try {
		const fields = [ 'staff_name', 'deptname', 'staff_type', 'personal_skill', 'is_special_job', 'resume_grade', 'is_skill_certificate', 'date_of_issue', 'enddate', 'org_name','post_name','memo'];
		const params = fd.data.params;
		delete params['page-no'];
		const data = generate_sort_fields(fd.data.url, fd.data.params as Params, fields);
		data.forEach((d) => {
			const field = d.field;
			render(fd.data.node, d, P02, field);
		});
	} catch (err) {
		console.error('------err', err);
	}
}

export default async function nba001(fd: IFeidaoAiNodejsComponent, ...args: any[]) {
	let page_no = fd.data.params['page-no'];
	if (!page_no) {
		page_no = 1;
	}
	let page_size = fd.data.params['page_size'];
	if (!page_size) {
		page_size = 20;
	}
	const q = p2q(fd.data.params as Params);
	const query = {
		filter: q.criteria,
		sort: q.sort,
		page_no,
		page_size
	};
	const data = generate_query4render(fd.data.url, fd.data.params as Params, name2title);
	render(fd.data.node, data, P01, 'P01');

	// 排序
	create_data_sort(fd);
	// 没有数据
	const res = await nodejs(fd.data.spaceid, "professional-find", query);
	// 无数据渲染P04
	if (res.dataArray.length == 0) {
		console.log(0)
		server_render(fd.data.node, 'P04', P04, '');
	} else {
		server_render(fd.data.node, 'P03', P03, res.dataArray);
	}

	res['page_size'] = page_size;
	render(fd.data.node, res, P05, 'P05');
}